﻿using System;
using System.Windows.Forms;
using System.Xml;

using ClassLibrary;
using Core;

namespace TestProject
{
    public partial class NewIndi : Form
    {
        private Population pop;
        private Model model;
        public NewIndi(Model m, Population p)
        {
            InitializeComponent();
            model = m;
            pop = p;
        }

        private void create_butt_Click(object sender, EventArgs e)
        {
            if(System.IO.File.Exists("file_input\\" + model.model_name + "\\Species\\" + pop.species.name + "\\Populations\\" + pop.name + "\\Individuals\\" + name_box.Text + ".xml"))
            {
                MessageBox.Show("An individual with this name already exists. Please, choose another name.");
                return;
            }
            int pairs = pop.species.sp_chromosome_table.chromosome_pairs_amount;
            System.IO.Directory.CreateDirectory("file_input\\" + model.model_name + "\\Species\\" +pop.species.name + "\\Populations\\" + pop.name+"\\Individuals");
            XmlTextWriter writer = new XmlTextWriter("file_input\\" + model.model_name + "\\Species\\" + pop.species.name + "\\Populations\\" + pop.name + "\\Individuals\\" + name_box.Text + ".xml", null);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteStartElement("individual");
            writer.WriteAttributeString("Name", name_box.Text);
            writer.WriteAttributeString("ChromosomePairsAmount",pairs.ToString());
            writer.WriteStartElement("indiviual_gene_set");
            for (int i = 0; i < pairs; i++)
            {
                writer.WriteStartElement("chromosome_pair");
                writer.WriteAttributeString("Id", pop.species.sp_chromosome_table.table[i].id.ToString());
                for (int k = 0; k < 2; k++)
                {
                    writer.WriteStartElement("chromosome");
                    writer.WriteAttributeString("Id", pop.species.sp_chromosome_table.table[i].id.ToString());
                    writer.WriteStartElement("gene_allels");
                    for (int j = 0; j < pop.species.sp_chromosome_table.table[i].amount_locuses; j++)
                    {
                        writer.WriteString("0");
                        if (j != pop.species.sp_chromosome_table.table[i].amount_locuses - 1)
                            writer.WriteString(" ");
                    }
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();

            }
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            MyXmlDocument doc = new MyXmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(pop.path));
            XmlElement el;
            el = doc.CreateElement("individual");
            el.SetAttribute("PathToIndividual", "file_input\\" + model.model_name + "\\Species\\" + pop.species.name + "\\Populations\\" + pop.name + "\\Individuals\\" + name_box.Text + ".xml");
            doc.Increment(doc.DocumentElement.Attributes[1]);
            doc.GetElementsByTagName("individuals")[0].AppendChild(el);
            doc.Save(pop.path);
            Functions.add_node_func(new Individual("file_input\\" + model.model_name + "\\Species\\" + pop.species.name + "\\Populations\\" + pop.name + "\\Individuals\\" + name_box.Text + ".xml", pop), name_box.Text, "indi");
            this.Close();
        }
    }
}
